import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 处理玩家回合行动类
 */
public class TurnManager {
    JButton Pass;/*过回合*/
    JButton PlayACard;//向队首出牌
    JButton PlatACardToTail;//向队尾出牌
    boolean isLocked;//非自身回合上锁，无法行动
    JLabel RivalCount;//对方手牌数量
    private static boolean isFirstTurn = true;//先手玩家必须出牌
    public static TurnManager Instance = new TurnManager();
    public static boolean isOver  = false;//游戏结束
    public static boolean RivalPassLastTurn = false;

    /**
     * 初始化
     */
    public void Init(){
        Pass = new JButton("过");
        PlayACard = new JButton("出牌(首)");
        PlatACardToTail = new JButton("出牌(尾)");
        RivalCount = new JLabel("对方骨牌数量:"+ RivalLibrary.Instance.DominoCount,SwingConstants.CENTER);
        RivalCount.setFont(new Font(null,Font.PLAIN,20));
        Pass.setBounds(1250,700,100,60);
        PlayACard.setBounds(1250,790,100,60);
        PlatACardToTail.setBounds(1250,880,100,60);
        RivalCount.setBounds(1080,25,220,60);
        Pass.setVisible(true);
        PlayACard.setVisible(true);
        PlatACardToTail.setVisible(true);
        RivalCount.setVisible(true);
        //RivalCount.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        PlayACard.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(isOver){
                    new NoBorderDialog("is Over");
                    return;
                }
                if(isLocked){
                    notYouTurnDialog();
                    return;
                }
                GamePanel.Instance.isFollowHead = true;
                GamePanel.Instance.isFollowTail= false;
                //System.out.println("Play a card clicked");
                DialogJudge("Head");
            }
        });
        PlatACardToTail.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //System.out.println("Play a card clicked");
                if(isOver){
                    new NoBorderDialog("is Over");
                    return;
                }
                if(isLocked){
                    notYouTurnDialog();
                    return;
                }
                GamePanel.Instance.isFollowTail = true;
                GamePanel.Instance.isFollowHead = false;
                DialogJudge("Tail");
            }
        });
        Pass.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(isOver){
                    new NoBorderDialog("is Over");
                    return;
                }
                if(isLocked) {
                    notYouTurnDialog();
                    return;
                }
                System.out.println("TurnManager Line63:"+isFirstTurn);
                if(isFirstTurn){
                    isFirstTurn = false;
                    new NoBorderDialog("首回合必须出牌！");
                    return;
                }
                if(isRivalPassLastTurn()){
                    int MyPoints = MyLibrary.Instance.CalePoints();
                    String msg = "Settlement " + MyPoints;
                    MainFrame.MyWriter.send(msg);//对方上回合也没牌可出，游戏结束开始结算
                }else{
                    MainFrame.MyWriter.send("Pass");
                }
                setLocked(true);//过牌 非自身回合 上锁
                WhoTurn.Instance.ChangeTextToRival();
            }
        });
    }

    /**
     * 根据行动生成相关提示的方法
     * @param TailOrHead 向队首还是队尾出牌用于向服务端传输己方出牌位置信息
     */
    public void DialogJudge(String TailOrHead) {
        if(MyLibrary.SelectedDominoes == null){
            //JOptionPane.showMessageDialog(null,"请选择一张骨牌！");
            new NoBorderDialog("请选择一张骨牌！");
            return;
        }
        if(!GamePanel.Instance.Judge(MyLibrary.SelectedDominoes)){
            new NoBorderDialog("接不上哦");
            return;
        }
        String msg = "put "+MyLibrary.SelectedDominoes.name +" "+TailOrHead;
        MainFrame.MyWriter.send(msg);
        GamePanel.Instance.PutDominoes(MyLibrary.SelectedDominoes,true);
        WhoTurn.Instance.ChangeTextToRival();
        setIsFirstTurn(false);
        setLocked(true);
    }

    /**
     * 提示非己方回合
     */
    private void notYouTurnDialog(){
        new NoBorderDialog("Not your turn");
    }

    /**
     * 上锁/解锁
     * @param locked 是否上锁
     */
    public void setLocked(boolean locked){
        isLocked = locked;
    }

    /**
     * 刷新对方骨牌数量
     */
    public void RivalCountUpdate(){
        String Count = String.valueOf(RivalLibrary.Instance.DominoCount);
        String s = "对方骨牌数量:";
        RivalCount.setText(s+Count);
        RivalCount.repaint();
    }
    public  static boolean isRivalPassLastTurn(){
        return RivalPassLastTurn;
    }
    public static void setRivalPassLastTurn(boolean flag){
        RivalPassLastTurn = flag;
    }
    public static void setIsFirstTurn(boolean is){
        isFirstTurn = is;
    }
}
